package com.ep.thirdPlatformProject.job.jobhandler;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.ep.thirdPlatformProject.job.service.DhgToOAService;
import com.ep.thirdPlatformProject.system.enums.OASyncLogEnum;
import com.ep.thirdPlatformProject.system.util.SimpleDealUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * DHG 人员信息生成员工单据的任务
 *
 * @author Karl
 * @date 2024/05/28
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class DhgToOAJob {


    private final DhgToOAService service;


    /**
     * 生成入职单
     */
    @XxlJob("submitOnBoardingFormJob")
    public void submitOnBoardingFormJob() {
        handleCommonJob("DHG->OA同步生成入职单", OASyncLogEnum.OA_SYNC_ONBOARDING_PERSON, true);
    }

    /**
     * 生成转岗流转单
     */
    @XxlJob("submitTransForm")
    public void submitTransForm() {
        handleCommonJob("DHG->OA同步生成转岗流转单", OASyncLogEnum.OA_SYNC_TRANS_POST_PERSON, true);
    }


    /**
     * 生成离职单
     */
    @XxlJob("submitLeaveForm")
    public void submitLeaveForm() {
        handleCommonJob("DHG->OA同步生成离职单", OASyncLogEnum.OA_SYNC_LEAVE_PERSON, true);
    }


    /**
     * 同步org并提交表单
     */
    @XxlJob("synOrgAndSubmitForm")
    public void synOrgAndSubmitForm() {
        handleCommonJob("DHG->OA同步组织架构并生成组织架构调整单",  OASyncLogEnum.OA_SYNC_ORG_LIST, false);
    }


    /**
     * 测试连接
     */
    @XxlJob("testConnection")
    @Deprecated
    public void testConnection() {
        handleCommonJob("测试连接正式库", (item) -> service.testConnection(), null);
    }


    /**
     * 通用的定时任务
     *
     * @param jobName       任务名称
     * @param paramSupplier param供应商
     * @param doJob         处理事件
     */
    @Deprecated
    private void handleCommonJob(String jobName, Function<Object, String> doJob, Supplier<Object> paramSupplier) {

        try {
            StopWatch watch = DateUtil.createStopWatch();
            watch.start("任务开始");

            String response = doJob.apply(Opt
                    .ofNullable(paramSupplier)
                    .orElse(Object::new)
                    .get());

            watch.stop();
            XxlJobHelper.log("任务结束，处理任务耗时：{}ms", watch.getTotalTimeMillis());
            XxlJobHelper.handleSuccess("\n" + response);
        } catch (Exception e) {
            log.error("{}定时任务执行，{}", jobName, e.getMessage(), e);
            XxlJobHelper.handleFail(
                    StrUtil.format("{}定时任务执行失败，{}", jobName, e.getMessage())
            );
        }

    }


    /**
     * 通用的定时任务
     *
     * @param jobName       作业名
     * @param oaSyncLogEnum Oa同步日志enum
     * @param dealEmpNos    处理空号
     */
    private void handleCommonJob(String jobName, OASyncLogEnum oaSyncLogEnum, Boolean dealEmpNos) {
        try {
            StopWatch watch = DateUtil.createStopWatch();
            watch.start("任务开始");
            String response;
            if (Boolean.TRUE.equals(dealEmpNos)) {
                String jobParam = XxlJobHelper.getJobParam();
                String[] empNos = SimpleDealUtil.paramValidateIsEmpNo(jobParam);
                response = service.submitCommonForm(oaSyncLogEnum, empNos);
            } else {
                response = service.submitCommonForm(oaSyncLogEnum);
            }
            watch.stop();
            XxlJobHelper.log("任务结束，处理任务耗时：{}ms", watch.getTotalTimeMillis());
            XxlJobHelper.handleSuccess("\n" + response);
        } catch (Exception e) {
            log.error("{}定时任务执行，{}", jobName, e.getMessage(), e);
            XxlJobHelper.handleFail(
                    StrUtil.format("{}定时任务执行失败，{}", jobName, e.getMessage())
            );
        }

    }

}
